<template>
	<view>
		<image src="../../static/3.jpg" mode="aspectFill" style="width: 750rpx;height: 320rpx;" />
		<view class="avatar_box">
			<image :src="user.avatar?user.avatar:'../../static/avatar.webp'" mode="" class="avatar" />
			<view class="follow" v-if="isShowFollow" @click="handleFollow">{{user.isFollow?'已关注':'关注'}}</view>
		</view>
		<view class="info_box">
			<view class="name">{{user.nickname?user.nickname:user.username}}</view>
			<view class="num_box">
				<text class="big" @click="handleTolist('follow')">{{user.followCount}}</text>
				<text class="black" @click="handleTolist('follow')">关注</text>
				<text class="big" @click="handleTolist('fens')">{{user.fenCount}}</text>
				<text class="black" @click="handleTolist('fens')">粉丝</text>
			</view>
			<view class="desc black">{{user.desc?user.desc:'暂无描述'}}</view>
		</view>
		<view class="switch_box">
			<view class="" :style="current===0?colorStyle:''" @click="handleTab(0)">作品</view>
			<view class="" :style="current===1?colorStyle:''" @click="handleTab(1)">收藏</view>
		</view>

		<swiper :style="'height:'+scrollHeight+'px;'" @change="swiperChage" :current="current">
			<swiper-item v-for="(n,index) in 2">
				<scroll-view :scroll-y="true" :style="'height:'+scrollHeight+'px;'"
					@scrolltolower="scrolltolower(index)">
					<view>
						<view class="separation-line"></view>
						<VideoList :item="item" v-for="item in list[index].data" />
						<view class="text" v-if="list[index].data.length===0"
							style="margin-top: 100rpx;font-size: 34rpx;text-align: center;">
							暂无数据</view>
						<view v-if="list[index].data.length > 10" hover-class="bg-light"
							style="text-align: center;color: #6c757d;margin-top: 10rpx;">
							{{ loadText }}
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import VideoList from '@/components/common/video-list.vue'
	export default {
		components: {
			VideoList
		},
		data() {
			return {
				current: 0,
				user_id: '',
				scrollHeight: '',
				user: {
					avatar: '',
					desc: '',
					sex: '',
					nickname: '',
					username: '',
					// favaCount: '',
					fenCount: '',
					followCount: '',
					// videoCount: ''
					isFollow: false
				},
				isShowFollow: true,
				colorStyle: {
					color: '#FB7299'
				},
				list: [{
					data: []
				}, {
					data: []
				}],
				page: [1, 1],
				url: ['/video_list/', '/fava_list/'],
				loadText: '',

			}
		},
		methods: {
			handleTab(value){
				this.current=value
			},
			handleTolist(type) {
				uni.navigateTo({
					url: `/pages/list/list?type=${type}&user_id=${this.user_id}`
				})
			},
			swiperChage(e) {
				this.current = e.detail.current
				
			},
			handleFollow() {
				this.$H.post('/user/follow', {
					follow: this.user_id
				}, {
					token: true,
					noJump: false,
					toast: true
				}).then(res=>{
					this.user.isFollow=!this.user.isFollow
				})
			},
			scrolltolower(index) {
				if (this.loadText === '没有更多了') {
					return
				}
				if (this.loadText === '正在加载中...') {
					return
				}
				this.loadText = '正在加载中...'
				this.page[index]++;
				console.log(this.page[index])
				this.getData(index)
			},
			getData(index) {

				this.$H.get(`${this.url[index]}${this.page[index]}?user_id=${this.user_id}`).then(res => {
					if (this.page[index] === 1) {
						this.list[index].data = res
					} else {
						this.list[index].data = [...this.list[index].data, ...res]
					}
					this.loadText = res.length === 10 ? '上拉加载更多' : '没有更多了'
				})

			},
			async getUserInfo() {
				let token = uni.getStorageSync('token')
				let res = null
				if (token) {
					res = await this.$H.get(`/user/user_info?user_id=${this.user_id}`, {
						token: true,
						noJump: false,
						toast: true
					})
				} else {
					res = await this.$H.get(`/user/user_info?user_id=${this.user_id}`)
				}
				this.user = res.user
				this.user.fenCount = res.favaCount
				this.user.followCount = res.followCount
				if (typeof(res.isFollow) === 'undefined') {
					this.isShowFollow = false
				} else {
					this.user.isFollow = res.isFollow
				}
			}
		},
		onLoad(e) {
			let res = uni.getSystemInfoSync()
			this.scrollHeight = res.windowHeight - 45 - res.statusBarHeight
			this.user_id = e.user_id
			this.getUserInfo()
			this.getData(0)
			this.getData(1)
		}
	}
</script>

<style lang="scss" scoped>
	.avatar_box {
		position: relative;
		margin: 0 30rpx;

		.avatar {
			margin-top: -50rpx;
			width: 132rpx;
			height: 132rpx;
			border-radius: 50%;
		}

		.follow {
			color: #fff;
			border-radius: 10rpx;
			position: absolute;
			right: 0;
			top: 0;
			background-color: #FB7299;
			width: 100rpx;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
		}
	}

	.info_box {
		padding: 0 40rpx 50rpx;
		border-bottom: 2rpx solid #dee2e6;

		.name {
			color: #FB7299;
			font-size: 40rpx;
			font-weight: 700;
		}

		.num_box {
			padding: 18rpx 0;

			text {
				margin-right: 10rpx;
			}
		}

		.noDesc {}

		.desc {
			max-height: 100rpx;
			text-overflow: ellipsis;
			/** 对象作为伸缩盒子模型显示 **/
			display: -webkit-box;
			/** 设置或检索伸缩盒对象的子元素的排列方式 **/
			-webkit-box-orient: vertical;
			/** 显示的行数 **/
			-webkit-line-clamp: 2;
			/** 隐藏超出的内容 **/
			overflow: hidden;


		}

		.gray {
			color: #414141;
		}

		.big {
			font-size: 32rpx;
		}

	}

	.switch_box {
		display: flex;
		height: 90rpx;
		align-items: center;

		view {
			font-size: 36rpx;
			text-align: center;
			flex: 1;

		}

	}
</style>
